package com.carleasoft.mps.cms.business.resource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.resource.bean.entity.ReasourceCollectEntity;
import com.carleasoft.mps.cms.business.resource.bean.entity.ReasourceEntity;
import com.carleasoft.mps.cms.business.resource.dao.ReasourceCollectMapper;
import com.carleasoft.mps.cms.business.resource.service.ReasourceCollectService;
import com.carleasoft.mps.cms.business.resource.service.ReasourceService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: yangHao
 * @createDate:2023/5/18
 * @description:
 */
@Service
public class ReasourceCollectServiceImpl extends ServiceImpl<ReasourceCollectMapper, ReasourceCollectEntity> implements ReasourceCollectService {
    @Autowired
    private ReasourceService reasourceService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCategory(ReasourceCollectEntity reasourceCollectEntity, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        IBeanUtil.paramVerify(reasourceCollectEntity, "categoryName");
        reasourceCollectEntity.setCreatePersonCode(user.getUserCode());
        reasourceCollectEntity.setCreatePersonName(user.getUserName());
        reasourceCollectEntity.setCreateTime(LocalDateTime.now());
        if (StrUtil.isNotBlank(reasourceCollectEntity.getCategoryCode())) {
            ReasourceCollectEntity collectEntity = this.getOne(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                    .eq(ReasourceCollectEntity::getCategoryCode, reasourceCollectEntity.getCategoryCode()));
            collectEntity.setCategoryName(reasourceCollectEntity.getCategoryName());
            collectEntity.setCreatePersonCode(user.getUserCode());
            collectEntity.setCreatePersonName(user.getUserName());
            collectEntity.setCreateTime(LocalDateTime.now());
            isOk = this.updateById(collectEntity);
        } else {
            reasourceCollectEntity.setCategoryCode(IdWorker.getIdStr());
            isOk = this.save(reasourceCollectEntity);
        }

        return isOk;
    }

    @Override
    public List<ReasourceCollectEntity> categoryList() {
        return this.list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(ReasourceCollectEntity reasourceCollectEntity) {
        List<ReasourceCollectEntity> collectEntity = new ArrayList<>();
        if (StrUtil.isBlank(reasourceCollectEntity.getCategoryCode())) {
            collectEntity = this.list(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                    .eq(ReasourceCollectEntity::getLibrary, reasourceCollectEntity.getLibrary())
                    .eq(ReasourceCollectEntity::getCategoryName, reasourceCollectEntity.getCategoryName()));
        } else {
            collectEntity = this.list(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                    .eq(ReasourceCollectEntity::getLibrary, reasourceCollectEntity.getLibrary())
                    .eq(ReasourceCollectEntity::getCategoryName, reasourceCollectEntity.getCategoryName())
                    .ne(ReasourceCollectEntity::getCategoryCode, reasourceCollectEntity.getCategoryCode()));
        }

        if (CollUtil.isNotEmpty(collectEntity)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(ReasourceCollectEntity reasourceCollectEntity) {
        int count = reasourceService.count(Wrappers.<ReasourceEntity>lambdaQuery()
                .eq(ReasourceEntity::getRCategory, reasourceCollectEntity.getCategoryCode()));
        if (count>0){
            throw new BusinessException(ExceptionCMSEnum.EXITS_NO);
        }

        return this.remove(Wrappers.<ReasourceCollectEntity>lambdaQuery()
                .eq(ReasourceCollectEntity::getCategoryCode, reasourceCollectEntity.getCategoryCode()));
    }
}
